//
//  ItemSwitchView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/31.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol ItemSwitchViewDelegate {
    func clickGesture(ui:UISwitch)
    func clickBiometric(ui:UISwitch)
}

class ItemSwitchView: UIView {
    
    // 手势
    private lazy var gestureView = SubItemSwitchView()
    // 生物类型
    private lazy var biometricView = SubItemSwitchView()
    // 设备支持类型
    private let supportType = BiometricUtil.shareInstance.checkSupport()
    
    // MARK: 代理
    public var delegate:ItemSwitchViewDelegate?
    
    public var resetGestureIsOn:Bool? {
        didSet {
            gestureView.switchView.isOn = resetGestureIsOn!
        }
    }
    
    public var resetBiometricIsOn:Bool? {
        didSet {
            biometricView.switchView.isOn = resetBiometricIsOn!
        }
    }
    
    override func layoutSubviews() {
        // 宽度
        let w = frame.width - MARGIN * 2
        // 手势
        gestureView.labelView.text = "手势"
        gestureView.frame = CGRect(x: MARGIN, y: 0, width: w, height: MARGIN70)
        addSubview(gestureView)
        // 添加开关事件
        gestureView.switchView.addTarget(self, action: #selector(clickGesture(ui:)), for: .valueChanged)
        // 下划线
        let line = UIView()
        line.backgroundColor = COLOR_LINE
        line.frame = CGRect(x: MARGIN, y: gestureView.frame.maxY, width: w, height: 1)
        addSubview(line)
        // 生物类型
        biometricView.frame = CGRect(x: MARGIN, y: line.frame.maxY, width: w, height: MARGIN70)
        // 添加开关事件
        biometricView.switchView.addTarget(self, action: #selector(clickBiometric(ui:)), for: .valueChanged)
        if supportType == 1 {
            // 指纹
            biometricView.labelView.text = "指纹"
            addSubview(biometricView)
        }
        else if supportType == 2 {
            // 人脸
            biometricView.labelView.text = "人脸"
            addSubview(biometricView)
        }
    }
    
    @objc func clickGesture(ui:UISwitch) -> Void {
        delegate?.clickGesture(ui: ui)
    }
    
    @objc func clickBiometric(ui:UISwitch) -> Void {
        delegate?.clickBiometric(ui: ui)
    }
    
    class SubItemSwitchView: UIView {
        // 开关
        public let switchView = UISwitch()
        // 文本
        let labelView = UILabel(title: "测试", color: COLOR_NICE, size: 15)
        
        override func layoutSubviews() {
            // 文本
            labelView.isUserInteractionEnabled = false
            labelView.frame = CGRect(x: 0, y: 0, width: MARGIN70, height: frame.height)
            addSubview(labelView)
            // 开关
            switchView.onTintColor = UIColor.orange
            let switchX = frame.width - switchView.bounds.width
            let switchY = (MARGIN70 - switchView.bounds.height) * 0.5
            switchView.frame.origin = CGPoint(x: switchX, y: switchY)
            addSubview(switchView)
        }
    }
}
